Gin
- 當要綁定
query param
時,struct tag 用form
- 當要綁定
body
時,struct tag 用json
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
route := gin.Default()
route.GET("/", test)
route.POST("/", test)
route.Run(":8000")
}
type Student struct {
Name string `form:"name"`
Age int `form:"age"`
}
func test(c *gin.Context) {
var student Student
err := c.ShouldBind(&student)
if err != nil {
fmt.Println("error: " + err.Error())
}
fmt.Printf("name: %v\n", student.Name)
fmt.Printf("age: %v\n", student.Age)
c.JSON(200, gin.H{"msg": http.StatusOK})
}